package com.lovo.cloudone.controller;

import com.lovo.cloudone.model.doo.MovieDO;
import com.lovo.cloudone.model.dto.ResultVo;
import com.lovo.utile.model.MeiTuanDTO;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * Description:
 *
 * @author WL
 * @date Created on 2020/11/30
 */
@RestController
public class MovieController {

    public  static List<MovieDO> movieList = new ArrayList<>();

    static {
            movieList.add(new MovieDO("八百",50,45));
            movieList.add(new MovieDO("美猴王",100,60));
    }

    @PostMapping("sell")
    public synchronized String sellMovie(@RequestBody MeiTuanDTO meiTuanDTO){
        String result = "不存在";

        for (MovieDO doo:movieList){

            if (doo.getMovieName().equals(meiTuanDTO.getMovieName())){
                if (doo.getMovieNum() - meiTuanDTO.getNum() >=0){
                    doo.setMovieNum(doo.getMovieNum() - meiTuanDTO.getNum());
                    result =  "购买成功,"+"取票编码为:"+ UUID.randomUUID() +"总价格为:"+meiTuanDTO.getNum() * doo.getMoviePrice();
                }
                else {
                    result = "没票了";
                }
            }

        }

        return result;
    }

}
